#include <LitStdAfx.h>
#include <LitFileParser.h>

namespace Lit
{
	namespace Editor
	{
		FileLineParser::FileLineParser( const std::string &filename, bool binary )
			: mRefLine( 0 )
		{
			open( filename, binary );
		}
		FileLineParser::FileLineParser( )
			: mRefLine( 0 )
		{
		}
		FileLineParser::~FileLineParser( )
		{
			close();
		}
		FileLineParser::operator bool() const{ return isGood(); }

		bool FileLineParser::isOpen() const{ return mStream.is_open(); }
		bool FileLineParser::isGood() const{ return mStream.good(); }
		size_t FileLineParser::getCurrentLine( ) const
		{
			return mRefLine;
		}
		bool FileLineParser::eof() const{ return mStream.eof(); }
		FileLineParser& FileLineParser::operator >>( Line& line )
		{
			if( !eof() )
			{
				std::getline( mStream, line.line );
				line.id = mRefLine;
				mRefLine ++;
			}
			return *this;
		}
		FileLineParser& FileLineParser::operator >>( std::string& line )
		{
			if( !eof() )
			{
				std::getline( mStream, line );
				mRefLine ++;
			}
			return *this;
		}
		bool FileLineParser::open( const std::string &filename, bool binary ){
			close();
			mStream.open( filename, (binary?(std::ios::in|std::ios::binary):(std::ios::in)) );
			return mStream.is_open();
		}
		bool FileLineParser::close()
		{
			if( mStream.is_open() ){ mStream.close(); return true; }
			return false;
		}
	};
};
